package org.shop.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.shop.dao.OrdersMapper;
import org.shop.entity.Orders;
import org.shop.service.OrderGoodsService;
import org.shop.service.OrdersService;
import org.shop.service.base.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OrdersServiceImpl extends BaseServiceImpl<Orders, Long> implements OrdersService {

	@Autowired
    private OrdersMapper ordersMapper;
	@Autowired
	private OrderGoodsService orderGoodsService;
	
	@Override
	public Orders getOrder(Map<String, Object> paramMap) {
		Orders order = ordersMapper.getOrder(paramMap);
		return order;
	}
	
	@Override
    public Map<String, Object> getShopOrderList(Map<String, Object> paramMap) {
		List<Map<String, Object>> orderDataList =new ArrayList<Map<String, Object>>();
		List<Orders> orderList = ordersMapper.getShopOrderList(paramMap);
        Integer count = ordersMapper.getShopOrderCount(paramMap);
        Map<String, Object> paramMap2=new HashMap<String, Object>();
        paramMap2.put("oders", orderList);
        List<Map<String, Object>> listMap=ordersMapper.getUserOrderGoodsList(paramMap2);
        for(Orders order:orderList){
        	Map<String, Object> orderMap=new HashMap<String, Object>();
        	List<Map<String, Object>> goodsDataList =new ArrayList<Map<String, Object>>();
        	for(Map<String, Object> dataMap:listMap){
        		if(dataMap.get("orderId").equals(order.getOrderId())){
        			Map<String, Object> map=new HashMap<String, Object>();
        			map.put("goodsName", dataMap.get("goodsName"));
        			map.put("goodsPrice", dataMap.get("goodsPrice"));
        			map.put("goodsThums", dataMap.get("goodsThums"));
        			map.put("goodsCount", dataMap.get("goodsCount"));
        			goodsDataList.add(map);
        		}
        	}
        	orderMap.put("orderId", order.getOrderId());
        	orderMap.put("orderNo", order.getOrderNo());
        	orderMap.put("orderPrice", order.getOrderPrice());
        	orderMap.put("orderStatus", order.getOrderStatus());
        	orderMap.put("orderId", order.getOrderId());
        	orderMap.put("userName", order.getUserName());
        	orderMap.put("userPhone", order.getUserPhone());
        	orderMap.put("createDate", order.getCreateDate());
        	orderMap.put("userAddress", order.getUserAddress());
        	orderMap.put("userGetCode", order.getUserGetCode());
        	orderMap.put("sendGoodsUserName", order.getSendGoodsUserName());
        	orderMap.put("sendGoodsUserPhone", order.getSendGoodsUserPhone());
        	orderMap.put("goods", goodsDataList);
        	orderDataList.add(orderMap);
        }
        
        //分页数据
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.put("totalCount", count);
        dataMap.put("list", orderDataList);
        dataMap.put("currentPage", paramMap.get("currentPage"));
        return dataMap;
    }
	
	
	@Override
	public Map<String, Object> getSupplierOrderList(Map<String, Object> paramMap) {
		List<Map<String, Object>> orderDataList =new ArrayList<Map<String, Object>>();
		List<Orders> supplierOrderList = ordersMapper.getSupplierOrderList(paramMap);
        Integer count = ordersMapper.getSupplierOrderCount(paramMap);
        Map<String, Object> paramMap2=new HashMap<String, Object>();
        paramMap2.put("oders", supplierOrderList);
        paramMap2.put("supplierId", paramMap.get("supplierId"));
        List<Map<String, Object>> listMap=ordersMapper.getUserOrderSupplierGoodsList(paramMap2);
        for(Orders order:supplierOrderList){
        	Map<String, Object> orderMap=new HashMap<String, Object>();
        	List<Map<String, Object>> goodsDataList =new ArrayList<Map<String, Object>>();
        	BigDecimal orderPrice=BigDecimal.ZERO;
        	for(Map<String, Object> dataMap:listMap){
        		if(dataMap.get("orderId").equals(order.getOrderId())){
        			Map<String, Object> map=new HashMap<String, Object>();
        			map.put("supplierGoodsName", dataMap.get("supplierGoodsName"));
        			Integer goodsCount=(Integer) dataMap.get("goodsCount");
        			BigDecimal supplierGoodsPrice=(BigDecimal) dataMap.get("supplierGoodsPrice");
        			BigDecimal price=supplierGoodsPrice.multiply(new BigDecimal(goodsCount));
        			orderPrice=orderPrice.add(price);
        			map.put("supplierGoodsPrice", dataMap.get("supplierGoodsPrice"));
        			map.put("goodsCount", dataMap.get("goodsCount"));
        			map.put("supplierGoodsThums", dataMap.get("supplierGoodsThums"));
        			goodsDataList.add(map);
        		}
        	}
        	orderMap.put("orderId", order.getOrderId());
        	orderMap.put("orderNo", order.getOrderNo());
        	orderMap.put("orderPrice", orderPrice);
        	orderMap.put("orderStatus", order.getOrderStatus());
        	orderMap.put("userName", order.getUserName());
        	orderMap.put("userPhone", order.getUserPhone());
        	orderMap.put("userAddress", order.getUserAddress());
        	orderMap.put("createDate", order.getCreateDate());
        	orderMap.put("orderunique", order.getOrderunique());
        	orderMap.put("goods", goodsDataList);
        	orderDataList.add(orderMap);
        }
        //分页数据
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.put("totalCount", count);
        dataMap.put("list", orderDataList);
        dataMap.put("currentPage", paramMap.get("currentPage"));
        return dataMap;
	}
	
	@Override
    public List<Map<String,Object>> getUserOrderList(Map<String, Object> paramMap) {
		List<Map<String, Object>> orderList = ordersMapper.getUserOrderList(paramMap);
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		for(int i=0;i<orderList.size();i++) {
			Map<String, Object> map = new HashMap<String, Object>();
			map = orderList.get(i);
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("orderId", (Long)map.get("orderId"));
			List<Map<String,Object>> orderGoodsList = orderGoodsService.getOrderGoodsList(params);
			BigDecimal orderPrice = BigDecimal.ZERO;
			for(int j=0;j<orderGoodsList.size();j++) {
				if(orderGoodsList.get(j).get("state").toString().equals("1")) {
					BigDecimal goodsPrice = new BigDecimal(orderGoodsList.get(j).get("goodsPrice").toString());
					BigDecimal goodsCount = new BigDecimal(orderGoodsList.get(j).get("goodsCount").toString());
					orderPrice = orderPrice.add(goodsPrice.multiply(goodsCount));
				}
			}
			map.put("orderGoodsList", orderGoodsList);
			map.put("newOrderPrice", orderPrice);
			list.add(map);
		}
        return list;
    }

	@Override
	public List<Map<String, Object>> getUnFinishAndFinishGoodsList(Map<String, Object> paramMap) {
		return ordersMapper.getUnFinishAndFinishGoodsList(paramMap);
	}

	@Override
	public int getUsersOfOrderCount(Long userId) {
		return ordersMapper.getUsersOfOrderCount(userId);
	}

	@Override
	public Map<String, Object> getOrderAndGoodsInfoByOrderNo(String orderNo) {
		return ordersMapper.getOrderAndGoodsInfoByOrderNo(orderNo);
	}

}
